/**
 * llmindex.io — per-domain leaderboard with sub-metrics (live-updating)
 * Author:  Simon-Pierre Boucher
 * Contact: contact@spboucher.ai
 * License: Proprietary — © Simon-Pierre Boucher, all rights reserved
 */
import Link from 'next/link';
import { notFound } from 'next/navigation';
import { DOMAINS, INDEX_VERSION, isDomain } from '@llmindex/scoring';
import { LiveLeaderboard, type ApiEntry, type ApiRun } from '@/components/LiveLeaderboard';
import { getLeaderboard } from '@/lib/data';

export const revalidate = 300;

export function generateStaticParams() {
  return DOMAINS.map((domain) => ({ domain }));
}

export default async function DomainLeaderboardPage({ params }: { params: { domain: string } }) {
  if (!isDomain(params.domain)) notFound();
  const data = await getLeaderboard(params.domain);
  const title = params.domain.replaceAll('_', ' ');

  const initialRun: ApiRun | null = data
    ? {
        id: data.run.id,
        kind: data.run.kind,
        status: data.run.status,
        created_at: new Date(data.run.createdAt).toISOString(),
        notes: data.run.notes,
      }
    : null;
  const initialEntries: ApiEntry[] = (data?.entries ?? []).map((e) => ({
    rank: e.rank,
    model: e.slug,
    name: e.name,
    provider: e.provider,
    score: e.score,
    score_low: e.scoreLow,
    score_high: e.scoreHigh,
    sub_metrics: e.subMetrics,
  }));

  return (
    <div className="space-y-6">
      <div className="flex flex-wrap items-center gap-3">
        <Link href="/" className="text-sm text-zinc-500 hover:text-zinc-900">
          ← Global
        </Link>
        <h1 className="text-xl font-bold capitalize text-zinc-900 sm:text-2xl">{title}</h1>
      </div>
      <LiveLeaderboard
        domain={params.domain}
        initialRun={initialRun}
        initialEntries={initialEntries}
        indexVersion={data?.run.indexVersion ?? INDEX_VERSION}
      />
    </div>
  );
}
